package com.zzw.springboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.springboot.bean.SysLog;
import com.zzw.springboot.mapper.SysLogMapper;
import com.zzw.springboot.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 系统日志Service实现类
 */
@Service
@Slf4j
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
    
    /**
     * 记录日志
     * @param sysLog 日志对象
     * @return 是否记录成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean saveLog(SysLog sysLog) {
        // 设置创建时间
        sysLog.setCreateTime(new Date());
        
        try {
            return this.save(sysLog);
        } catch (Exception e) {
            log.error("保存日志失败：{}", e.getMessage(), e);
            return false;
        }
    }
    
    /**
     * 异步保存日志
     * @param sysLog 日志对象
     */
    @Async
    @Override
    public void asyncSaveLog(SysLog sysLog) {
        saveLog(sysLog);
    }
} 